<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Qrcode extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        
        // dump(db('corporation')->where('id',31)->find());die;
        $GLOBALS['data'] = input('get.');
        if (array_key_exists('code',$GLOBALS['data']) && array_key_exists('type',$GLOBALS['data'])) {
            // 电梯、部件、公司二维码
            if (!in_array($GLOBALS['data']['type'], ['1','2','3'], true)) {
                $this->error('二维码异常 - 类型错误', $this->request->server('HTTP_REFERER', '', 'trim'));
            }
            /*if (mb_strlen($GLOBALS['data']['code']) !== 32) {
                $this->error('二维码异常 - 码值长度错误', $this->request->server('HTTP_REFERER', '', 'trim'));
            }*/
            $type_index = ['1'=>['电梯','elevator_basic_info'],'2'=>['部件','elevator_components_info'],'3'=>['公司','corporation']];
            // 需要查询的基础信息
            $info = Db::name($type_index[$GLOBALS['data']['type']][1])->find(function($query){
                switch ($GLOBALS['data']['type']) {
                    case '1':
                        $query->where('code',$GLOBALS['data']['code'])->field('factory_serial_number as \'出厂编号\',factory_date as \'出厂日期\',rated_load_capacity as \'额定载重量\'');
                        break;
                    case '2':
                        $query->where('code',$GLOBALS['data']['code'])->field('component_name as \'部件名称\',model as \'部件型号\',product_number as \'部件产品编号\',manufacturer as \'部件制造单位\'');
                        break;
                    case '3':
                        $query->where('Qrcode_value',$GLOBALS['data']['code'])->field('name as \'公司名称\',region as \'公司地区\',present as \'公司地址\',phone as \'公司电话\',code as \'公司代码字段\'');
                        break;
                }
            });
            
            if (!$info) {
                $this->error('二维码异常 - 档案未找到', $this->request->server('HTTP_REFERER', '', 'trim'));
            }
            if (array_key_exists('view',$GLOBALS['data'])) {
                if ($GLOBALS['data']['view'] === 'json') {
                    return json([
                        'type' => $type_index[$GLOBALS['data']['type']],
                        'info' => $info,
                        'code' => $GLOBALS['data']['code']
                    ]);
                }
            }
            $this->view->assign([
                'type' => $type_index[$GLOBALS['data']['type']],
                'info' => $info,
                'code' => $GLOBALS['data']['code']
            ]);
        } else if (false) {
            // 其他类型二维码
        } else {
            // 异常二维码值
            $this->error('未知二维码', $this->request->server('HTTP_REFERER', '', 'trim'));
        }
        return $this->view->fetch();
    }

    // 电梯档案
    public function elevator_basic_info()
    {
        $code = input('get.code');
        if (!$code) {
            $this->error('参数错误', $this->request->server('HTTP_REFERER', '', 'trim'));
        }
        $data = Db::name('elevator_basic_info')->field('code as qrCode,pinzhong,factory_serial_number,factory_date,product_model,rated_speed_up,rated_speed_down,rated_load_capacity,lifting_height,explosion_proof_type,drive_motor_power,drive_motor_speed,reduction_ratio,hydraulic_pump_station_pressure,suspension_medium_type,suspension_medium_quantity,suspension_medium_model,suspension_medium_specification,posttime,input_clerk,characteristic_number,manufacturing_unit,installation_unit,usage_registration,change_registration,checkinfo')->where('code',$code)->find();
        if ($data['checkinfo'] == 'true') {
            $data['checkinfo'] = '正常';
        } else {
            $data['checkinfo'] = '异常';
        }
        $data = $this->translate($data);
        if (!$data) {
            $this->error('电梯未找到', $this->request->server('HTTP_REFERER', '', 'trim'));
        }
        $this->view->assign([
            'type' => '电梯',
            'data' => $data
        ]);
        return $this->view->fetch('view');
    }

    // 部件档案
    public function elevator_components_info()
    {
        $code = input('get.code');
        if (!$code) {
            $this->error('参数错误', $this->request->server('HTTP_REFERER', '', 'trim'));
        }
        $data = Db::name('elevator_components_info')->field('Code as qrCode,Component_name,Model,Product_number,Manufacturer')->where('code',$code)->find();
        $data = $this->translate($data);
        if (!$data) {
            $this->error('部件未找到', $this->request->server('HTTP_REFERER', '', 'trim'));
        }
        $this->view->assign([
            'type' => '部件',
            'data' => $data
        ]);
        return $this->view->fetch('view');
    }

    // 公司档案
    public function corporation()
    {
        $code = input('get.code');
        if (!$code) {
            $this->error('参数错误', $this->request->server('HTTP_REFERER', '', 'trim'));
        }
        $data = Db::name('corporation')->field('Qrcode_value as qrCode,Name,Region,Present,Phone,Code,Expiration_date')->where('Qrcode_value',$code)->find();
        if (!$data) {
            $this->error('公司未找到', $this->request->server('HTTP_REFERER', '', 'trim'));
        }
        $data = $this->translate($data);
        $this->view->assign([
            'type' => '公司',
            'data' => $data
        ]);
        return $this->view->fetch('view');
    }

    protected function translate($data)
    {
        foreach ($data as $k => $v) {
            $data[$k] = ['name' => __($k), 'value' => $v];
        }
        return $data;
    }
}